package com.example.manager.controller;

import com.example.manager.domain.R;
import com.example.manager.domain.SysMenu;
import com.example.manager.domain.SysUser;
import com.example.manager.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/sysMenu")
@RestController
public class SysMenuController {

    @Autowired
    SysMenuService sysMenuService;

    @GetMapping("/tree")
    public R<List<SysMenu>> findSysMenusTree(@RequestAttribute("user") SysUser users){
        if (users.getUserId()==1){//超级管理员的左侧树形，不需要进行权限关联查询，直接显示menus里面的所有数据
            List<SysMenu> menusTree = sysMenuService.findSysMenuTree(0);
            return R.success(menusTree);
        }else {
            List<SysMenu> menusTreeByUserId = sysMenuService.findSysMenuTreeByUserId(users.getUserId(),0);
            return R.success(menusTreeByUserId);
        }
    }

    @GetMapping("/all")
    public R<List<SysMenu>> findSysMenus(@RequestAttribute("user") SysUser user){
        if (user.getUserId()==1){//超级管理员
            return R.success(sysMenuService.findSysMenus());
        }else {//普通用户
            return R.success(sysMenuService.findSysMenusByUserId(user.getUserId()));
        }
    }

    //查询所有权限属性菜单
    @GetMapping("/findSysMenusTree")
    public R<List<SysMenu>> findSysMenusTree(){
        List<SysMenu> menus = sysMenuService.findSysMenusTreeNM(0);
        return R.success(menus);
    }
    //查询当前角色拥有哪些功能权限，不要菜单
    @GetMapping("/findBySys_RoleId")
    public R<List<SysMenu>> findBySys_RoleId(Integer roleId){
        List<SysMenu> menus = sysMenuService.findBySys_RoleId(roleId);
        return R.success(menus);
    }

}
